const orderService = require('../services/orderService')
const createOrderNum = require('../util/createOrderNum')

const shopController = {
  async addOrder(req, res, next) {
    try {
      let data = await orderService.addOrder({ ...req.body, user: req.auth.id, order_num: createOrderNum() })
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async getUserOrder(req, res, next) {
    try {
      let data = await orderService.getUserOrder(req.auth.id)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async getOrder(req, res, next) {
    try {
      let data = await orderService.getOrder()
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async updateOrder(req, res, next) {
    try {
      let data = await orderService.updateOrder(req.body._id)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async deleteOrder(req, res, next) {
    try {
      let data = await orderService.deleteOrder(req.body._id)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
}

module.exports = shopController
